package com.ITArmy.service.impl;

import com.ITArmy.beans.Exchange;
import com.ITArmy.beans.Order;
import com.ITArmy.beans.User;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.ExchangeMapper;
import com.ITArmy.dao.SerializeMapper;
import com.ITArmy.dao.UserMapper;
import com.ITArmy.service.ExchangeService;
import com.ITArmy.service.OrderService;
import com.ITArmy.service.PayService;
import com.ITArmy.util.DateChange;
import com.ITArmy.util.GetUserSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;


@Service
public class PayServiceImpl implements PayService {
    private static Logger logger = Logger.getLogger(PayServiceImpl.class);
    GetUserSession getUserSession = new GetUserSession();
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderService orderService;
    @Autowired
    ExchangeMapper exchangeMapper;
    @Autowired
    SerializeMapper serializeMapper;
    DateChange dateChange = new DateChange();

    //用户开通会员（金币）
    @Override
    public ServerResponse openVip(HttpServletRequest request, int typeId) {
        User user = getUserSession.getUser(request);
        Exchange exchange = (Exchange) exchangeMapper.selectByPrimaryKey(1);
        double price;
        int e_convert = exchange.geteConvert();
        switch (typeId) {
            case 1:
                price = exchange.geteVipPrice1();
                break;
            case 3:
                price = exchange.geteVipPrice3();
                break;
            case 6:
                price = exchange.geteVipPrice6();
                break;
            case 12:
                price = exchange.geteVipPrice12();
                break;
            default:
                return ServerResponse.createByErrorMesage("请输入正确的月数....");
        }
        long goldPrice = (long) price * e_convert;
        if (user.getuBalance() == null) {
            return ServerResponse.createByErrorMesage("您的余额为0...");
        }
        if (user.getuBalance() < goldPrice) {
            logger.info("邮箱为.." + user.getuEmail());
            logger.info("需要.." + goldPrice);
            logger.info("余额.." + user.getuBalance());
            return ServerResponse.createByErrorMesage("您的余额不足请充值...");
        }
        logger.info("邮箱为" + user.getuEmail());
        logger.info("需要" + goldPrice);
        logger.info("余额" + user.getuBalance());
        //往订单里面添加数据
        Order order = new Order();
        order.setuId(user.getuId());
        order.setsGold(goldPrice);
        orderService.addOrder(request,order);

        try {
            if (user.getuVip() == "1") {
                user.setuExpire(dateChange.addVipTime(user.getuExpire(), typeId));
            } else {
                user.setuExpire(dateChange.addVipTime(new Date(), typeId));
            }
            //给用户添加vip
            user.setuVip("1");
            //金币减少goldPrice
            user.setuBalance(user.getuBalance() - goldPrice);
            //更新一下用户的信息
            int resultCount = userMapper.updateByPrimaryKeySelective(user);
            if (resultCount < 1) {
                return ServerResponse.createByErrorMesage("购买失败...");
            }
        } catch (Exception e) {
            return ServerResponse.createByErrorMesage("购买失败...");
        }
        return ServerResponse.createBySuccessMessage("购买成功...");
    }

    //用户购买连载（金币）
    @Override
    public ServerResponse buySerialize(HttpServletRequest request, long serializeId) {
        Exchange exchange = (Exchange) exchangeMapper.selectByPrimaryKey(1);
        User user = getUserSession.getUser(request);
        Order order = new Order();
        order.setsTime(new Date());
        order.setuId(user.getuId());
        //需要增加个订单的类型(还没写)
        double balance = user.getuBalance();
        double price = serializeMapper.selectByPrimaryKey(serializeId).getsPrice();
        int e_convert = exchange.geteConvert();//汇率
        long goldPrice = (long) price * e_convert;//需要的金币
        if (balance < goldPrice) {
            return ServerResponse.createByErrorMesage("您的余额不足，您还有" + balance + "个金币，请积极发表文章赚取金币哦~");
        }
        order.setsGold(goldPrice);
        //修改用户的余额
        double backbalance = user.getuBalance() - goldPrice;
        user.setuBalance(backbalance);
        userMapper.updateByPrimaryKeySelective(user);
        //进行添加订单
        orderService.addOrder(request,order);
        return ServerResponse.createBySuccessMessage("购买成功，您现在的余额为" + backbalance);

    }
}
